<template>
  <div>
<!--    添加div-->
    <div class="permissManaTool">
      <el-input size="small" placeholder="请输入角色英文名" v-model="addRole.name">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input size="small" placeholder="请输入角色中文名" v-model="addRole.namezh"
                @keydown.enter.native="doAddRole"></el-input>
      <el-button type="primary" size="small" icon="el-icon-plus" @click="doAddRole">添加角色</el-button>
    </div>
    <div class="permissManaMain">
      <el-collapse v-model="activeName" accordion @change="change">
        <el-collapse-item :title="r.namezh" :name="r.id" v-for="(r,index) in roles" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问的资源</span>
              <el-button style="float: right; padding: 3px 0;color: #ff0000;" icon="el-icon-delete"
                         type="text" @click="deleteRole(r)"></el-button>
            </div>
            <div>
              <el-tree
                  show-checkbox
                  node-key="id"
                  ref="tree"
                  :key="index"
                  :default-checked-keys="selectedMenus"
                  :data="allmenus" :props="defaultProps"></el-tree>
              <div style="display: flex;justify-content: flex-end">
                <el-button @click="cancelUpdate">取消修改</el-button>
                <el-button type="primary" @click="doUpdate(r.id,index)">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
import {getRequest} from "../../../utils/api";
import {postRequest} from "../../../utils/api";
import {putRequest} from "../../../utils/api";
import {deleteRequest} from "../../../utils/api";

export default {
  name: "PermissMana",
  data(){
    return{
      allmenus: [],
      selectedMenus: [],
      addRole:{
        name:'',
        namezh:'',
      },
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      roles:[],
      activeName:-1,
    }
  },
  methods:{
    deleteRole(role){
      this.$confirm('此操作将永久删除【' + role.namezh + '】角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRequest("/system/basic/permiss/role/" + role.id).then(resp => {
          if (resp) {
            this.initRoles();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    change(rid){
      if(rid){
        this.initAllMenus();
        this.initMidsByRid(rid);
      }
    },
    doUpdate(rid,index){
      let tree = this.$refs.tree[index];
      let selectedKeys = tree.getCheckedKeys(true);
      let url = "/system/basic/permiss/?rid="+rid;
      selectedKeys.forEach(key=>{
        url += '&mids='+key;
      })
      putRequest(url).then(res=>{
        if(res){
          this.activeName = -1;
        }
      })
    },
    cancelUpdate(){
      this.activeName = -1;
    },
    doAddRole(){
      if(this.addRole.name){
        postRequest("/system/basic/permiss/role",this.addRole).then((res)=>{
          if (res){
            this.addRole={};
            this.initRoles();
          }
        })
      }
    },
    initRoles(){
      getRequest("/system/basic/permiss/").then((res)=>{
        if(res){
          this.roles = res;
        }
      })
    },
    initAllMenus() {
      getRequest("/system/basic/permiss/menus").then(resp => {
        if (resp) {
          this.allmenus = resp;
        }
      })
    },
    initMidsByRid(rid){
      getRequest("/system/basic/permiss/mids/"+rid).then(resp => {
        if (resp) {
          this.selectedMenus = resp;
        }
      })
    },
  },
  mounted() {
    this.initRoles();
  }
}
</script>

<style >
.permissManaTool {
  display: flex;
  justify-content: flex-start;
}

.permissManaTool .el-input {
  width: 300px;
  margin-right: 6px;
}
.permissManaMain{
  width: 700px;
  margin-top: 10px;
}
</style>